package site.wetsion.campusgrade.login;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import java.net.URL;
import javax.imageio.ImageReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
import com.gargoylesoftware.htmlunit.html.HtmlTableHeader;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

import site.wetsion.campusgrade.abs.AbstractClient;
import site.wetsion.campusgrade.codeimg.Tess4jDiscern;

public class Login extends AbstractClient{

//	private final static Logger log = LoggerFactory.getLogger(Login.class);
	private static final String URL = "http://172.16.88.101:9001";
	
	public void login(){

		WebClient client = initWebClient();
		System.out.println("测试账号1307070205");
		try {
			URL url = new URL(URL);
			WebRequest request = new WebRequest(url);
			HtmlPage page = client.getPage(request);
			final HtmlForm form = page.getFormByName("loginForm");
			
			HtmlTextInput input_username = form.getInputByName("zjh");
			input_username.setText("1307070210");
			HtmlPasswordInput input_pwd = form.getInputByName("mm");
			input_pwd.setText("zc520.@");
			
			HtmlImage image = (HtmlImage) page.getElementById("vchart");
			ImageReader imageReader = image.getImageReader();
			BufferedImage bufferedImage = imageReader.read(0);
			
			String code = Tess4jDiscern.getCode(bufferedImage);
			System.out.println("验证码："+code);
			
			HtmlTextInput codeinput = form.getInputByName("v_yzm");
			codeinput.setText(code);
			
			HtmlElement loginbtn = (HtmlElement) page.getElementById("btnSure");
			
			page = loginbtn.click();
//			System.out.println(page.asXml());
			String targeturl = "http://172.16.88.101:9001/loginAction.do";
			String forwardurl = page.getUrl().toString().trim();
			if(page.asText().contains("个人管理")){
				System.out.println("登录成功");
				
//				String gradeurl = "http://172.16.88.101:9001/gradeLnAllAction.do?type=ln&oper=qbinfo&lnxndm=2016-2017%D1%A7%C4%EA%C7%EF(%C1%BD%D1%A7%C6%DA)";
				String gradeurl = "http://172.16.88.101:9001/gradeLnAllAction.do?type=ln&oper=sxinfo&lnsxdm=001";
				final HtmlPage gradepage = client.getPage(gradeurl); 
//				System.out.println(gradepage.asXml());
				
				List<HtmlTable> tables = (List<HtmlTable>) gradepage.getByXPath("//table[@class='titleTop2']");
				for (HtmlTable htmlTable : tables) {
					//表格头部
//					HtmlTableHeader header = htmlTable.getHeader();
//					List<HtmlTableRow> headrows = header.getRows();
//					for (HtmlTableRow headrow : headrows) {
//						List<HtmlTableCell> headcells = headrow.getCells();
//						for (HtmlTableCell headcell : headcells) {
//							System.out.print(headcell.asText());
//						}
//						System.out.println();
//					}
					//表格身体
					List<HtmlTableRow> rows = htmlTable.getRows();
					for (HtmlTableRow row : rows) {
						List<HtmlTableCell> cells = row.getCells();
						for (HtmlTableCell cell : cells) {
							System.out.print(cell.asText());
						}
						System.out.println();
					}
				}
				
			}else{
				System.out.println("登录失败");
			}
			page.cleanUp();
			client.closeAllWindows();
		} catch (FailingHttpStatusCodeException e) {
//			log.error(e.getMessage());
		} catch (MalformedURLException e) {
//			log.error(e.getMessage());
		} catch (IOException e) {
//			log.error(e.getMessage());
		}
	
	}
	
	public static void main(String[] args) {
		Login login = new Login();
		login.login();
	}
	
}
